<?php

/**
 * dealcategory
 *
 **/
class Globalconfig_Model_Dealcategory extends Zend_Db_Table_Abstract
{
	public $_name = "dealcategory";
    public function __construct($config = array())
    {
        $this->_db = Zend_Db_Table::getDefaultAdapter();
        parent::__construct($config);
    }
	
	public function count($parameter) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$where = "(1=1)";
		
		$sql = "select count(*) as COUNT from " . $this->_name
				. " U WHERE $where";
		$r = $db->query($sql)->fetch();
		return $r['COUNT'];
	}
	public function SelectAll($parameter, $limit, $offset) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$where = "(1=1)";
		$sql = "SELECT 
					DEALCATEGORYID,
					NAME,
					PARENTID,
					STATUS,
					(	SELECT COUNT(*) 
						FROM " . $this->_name
						. " AS SUB
						WHERE SUB.PARENTID = U.DEALCATEGORYID
					) AS SUBCOUNT
				FROM " . $this->_name. " U 
				WHERE $where limit $offset,$limit
	                ";
		$r = $db->query($sql)->fetchAll();
		return $r;
	}
	public function SelectAllNoPaging() {
		$db = Zend_Db_Table::getDefaultAdapter();
		
		$sql = "SELECT 
					DEALCATEGORYID,
					NAME,
					PARENTID,
					STATUS
				FROM " . $this->_name;
		$r = $db->query($sql)->fetchAll();
		return $r;
	}
	
	public function SelectAllNoPagingById($id) {
		$db = Zend_Db_Table::getDefaultAdapter();
		
		$sql = "SELECT 
					DEALCATEGORYID,
					NAME,
					PARENTID,
					STATUS
				FROM " . $this->_name
				." WHERE DEALCATEGORYID !=".$id				
				." AND PARENTID  !=".$id;				
		
		$r = $db->query($sql)->fetchAll();
		return $r;
	}
	
	public function AddNewDealCategory($params) {
		$id = $params['dealcategoryid'];
		$db = Zend_Db_Table::getDefaultAdapter();
		try {
			if($id ==0){
							$data = array(
								'NAME' => $params['name'],
								'PARENTID' => $params['parentid'],
								'STATUS' => $params['status']
						);
						$db->insert($this->_name, $data);
			}
			else{
					$data = array(
				            'DEALCATEGORYID' => $params['dealcategoryid'],
				            'NAME' => $params['name'],
							'PARENTID' => $params['parentid'],
							'STATUS' => $params['status']
		);
				$db->update($this->_name, $data,"DEALCATEGORYID = ".$id);
			}
		}catch (Zend_Exception $e) {
			echo ($e->getMessage());
			exit;
		}
	}
	
	public function DeleteDealCategory($id){
		$db = Zend_Db_Table::getDefaultAdapter();
		$where = $db->quoteInto('DEALCATEGORYID = ?', $id);
		$db->delete($this->_name, $where);
	}
	
	public function FindById($id) {
		$result = $this->getDefaultAdapter()
		->query("SELECT*FROM $this->_name WHERE DEALCATEGORYID = ?", $id);
		return $result->fetch();
	}
	
	public function CheckExitsID($id) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$sql = "select * from $this->_name where DEALCATEGORYID='" . $id . "'";
		$stmt = $db->query($sql);
		$r = $stmt->fetch();
		if ($r)
		return 1;
		else
		return 0;
	}

	public function CheckExistsName($name) {
		if($name == '') {
			return 0;
		}
		else 
		{
			$db = Zend_Db_Table::getDefaultAdapter();
			$sql = "select * from $this->_name where NAME='" . $name . "'";
			$stmt = $db->query($sql);
			$r = $stmt->fetch();
			if ($r)
			return 1;
			else
			return 0;
		}
	}
}

